
package com.xtasz.itom.restfull.controller.alarm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.xtasz.itom.alarm.domain.Alarm;
import com.xtasz.itom.alarm.service.inter.alarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.series.Pie;
import com.github.abel533.echarts.json.GsonOption;

@Controller
@RequestMapping("/alarm")
public class AlarmController {
	@Autowired
	private com.xtasz.itom.alarm.service.inter.alarmService alarmService;
    /*
     * 告警级别占比（饼图）
     * */
	@RequestMapping(value="/getAlarmProportionInfo" , method=RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> getAlarmProportionInfo() throws Exception {
		 List<HashMap<String, Object>> list = alarmService.getAlarmProportionInfo(null);//获取结果集
		 HashMap<String, Object> map =new HashMap<String, Object>();
		 map.put("option",getGsonOption(list).toString());
		 map.put("datalist", list);
		return map;
	}  
	 /*
     * 活动告警列表
     * */
	@RequestMapping(value="/getAlarmList" , method=RequestMethod.POST)
    public  List<Alarm> getAlarmList() throws Exception {
		List<Alarm> list = alarmService.getAlarmlist(null);//获取结果集
		return list;
	}  
	/*
	 * 历史告警列表
	 * */
	@RequestMapping(value="/getNegativeAlarmList" , method=RequestMethod.POST)
	public  List<Alarm> getNegativeAlarmList() throws Exception {
		List<Alarm> list = alarmService.getNegativeAlarmList(null);//获取结果集		 
		return list;     	
	}  
	/*
     * 告警详细信息
     * */
	@RequestMapping(value="/getSingleAlarm" , method=RequestMethod.POST)
    public @ResponseBody List getSingleAlarm(@RequestBody HashMap param) throws Exception {
		return  alarmService.getSingleAlarm(param);     	
	}
	/*
	 * 告警Topology相关信息
	 * */
	@RequestMapping(value="/getAlarmTopology" , method=RequestMethod.POST)
	public @ResponseBody HashMap getAlarmTopology(@RequestBody HashMap param) throws Exception {
		return  alarmService.getAlarmTopology(param);     	
	}
	/*
	 * 告警TopologyInterface相关信息
	 * */
	@RequestMapping(value="/getAlarmTopologyInterfaceInfo" , method=RequestMethod.POST)
	public @ResponseBody HashMap getAlarmTopologyInterfaceInfo(@RequestBody HashMap param) throws Exception {
		return  alarmService.getAlarmTopologyInterfaceInfo(param);     	
	}
	/*
	 * 告警TopologyInterface错误二级码详细相关信息
	 * */
	@RequestMapping(value="/getInterfaceErrInfo" , method=RequestMethod.POST)
	public  List getInterfaceErrInfo(@RequestBody HashMap param) throws Exception {
	  return  alarmService.getInterfaceErrInfo(param);     	
	}
	/*
     * 告警接口标示
     * */
	@RequestMapping(value="/getAlarmInterfaceFlag" , method=RequestMethod.POST)
    public   List getAlarmInterfaceFlag(@RequestBody HashMap param) throws Exception {
		 List list = alarmService.getAlarmInterfaceFlag(param);//获取结果集		 
		return list;     	
	}  
	/*
	 * 错误二级码TOP10
	 * */
	@RequestMapping(value="/getFailCodeTop10" , method=RequestMethod.POST)
	public  List getFailCodeTop10(@RequestBody HashMap param) throws Exception {
		List list = alarmService.getFailCodeTop10(param);//获取结果集
		return list;     	
	}  
	/*
	 * 接口调用详情
	 * */
	@RequestMapping(value="/getInterfaceInfoDetailReg" , method=RequestMethod.POST)
	public @ResponseBody List getInterfaceInfoDetailreg(@RequestBody HashMap param) throws Exception {
		List<String> listparam=new ArrayList<String>();
		listparam=Arrays.asList(((String) param.get("SERVICE_NAME")).split(","));
    	param.put("SERVICE_NAMES", listparam);
		List list =  alarmService.getInterfaceInfoDetailReg(param);   
		return list;
	}
	
	
	public String getGsonOption(List<HashMap<String, Object>> list){
		  
		  GsonOption option = new GsonOption();
		  option.animation(true);//是否开启动画
		  option.title().text("当日告警类型占比").x(X.center);
		  option.tooltip().trigger(Trigger.item).formatter("{a} <br/>{b} : {c} ({d}%)");
		  option.calculable(true);
		  
		  Pie pie = new Pie();
		  pie.name("告警类型");
		  pie.type(SeriesType.pie);
		  pie.radius("55%");
		  pie.center("50%", "60%");
		  for(int i = 0;i<list.size();++i){
			  option.legend().orient(Orient.vertical).x(X.left).data(list.get(i).get("EVENT_CLASS"));//设置图例
			  pie.data(new PieData((String) list.get(i).get("EVENT_CLASS"), list.get(i).get("ALARMNUM")));  //饼图value
		  }
		  option.series(pie);
		  return option.toString();
	}
		
	
	/*******************************************************************************/
	public alarmService getAlarmService() {
		return alarmService;
	}
	public void setAlarmService(alarmService alarmService) {
		this.alarmService = alarmService;
	} 
	
}
